﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics.Contracts;
using SimpleIR;

namespace PInfoModel
{
	/// <summary>
	/// This class contains maps from caller id/escape info to callee ids and callee escinfo to caller escinfo
	/// reference kinds.
	/// </summary>
	public sealed class CallerCalleeIDMapping : IEquatable<CallerCalleeIDMapping>
	{
		public readonly int CallerLine;

		public readonly IRInvokeIdentity CallerInvokeIdentity;
		public readonly int CallerContextID;

		public readonly IRInvokeIdentity CalleeInvokeIdentity;
		public int CalleeContextID;

		public Dictionary<MemLocID, HashSet<MemLocID>> CallerIDToCalleeIDMap;
		
		public CallerCalleeIDMapping(int callerline, IRInvokeIdentity callerinvoke, int callerid, IRInvokeIdentity calleeinvoke)
		{
			this.CallerLine = callerline;

			this.CallerInvokeIdentity = callerinvoke;
			this.CallerContextID = callerid;

			this.CalleeInvokeIdentity = calleeinvoke;
			this.CalleeContextID = -1;

			this.CallerIDToCalleeIDMap = new Dictionary<MemLocID, HashSet<MemLocID>>();
		}

		public override string ToString()
		{ return this.CallerInvokeIdentity.ToString() + "@" + this.CallerContextID + " -> " + this.CalleeInvokeIdentity.ToString() + "@" + this.CalleeContextID; }
		

		public override bool Equals(object obj)
		{ return (obj is CallerCalleeIDMapping) && this.Equals((CallerCalleeIDMapping)obj); }

		public override int GetHashCode()
		{ return this.CallerLine ^ this.CalleeContextID ^ this.CalleeContextID; }

		public bool Equals(CallerCalleeIDMapping other)
		{ return (this.CallerLine == other.CallerLine) & (this.CallerContextID == other.CallerContextID) & (this.CalleeContextID == other.CalleeContextID); }
	}
}
